﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using E1;
namespace F11
{
   
    public partial class Form1 : Form
    {
        OrderService orderService;
        BindingSource detailbindingSource;
        BindingSource orderBindingSource;
        List<orderinfo> orderinfos;
        orderinfo currentinfo;
        int checkedRadio;
        NewDialog newDialog;
        ChangeDialog changeDialog;
        public Form1()
        {
            InitializeComponent();
            orderinfos = new List<orderinfo>();
            
            detailbindingSource = new BindingSource();
            orderBindingSource = new BindingSource();
            currentinfo = new orderinfo();
            

            orderService = new OrderService();
            List<OrderDetail> details = new List<OrderDetail>();
            details.Add(new OrderDetail(1, 1, 1));
            details.Add(new OrderDetail(2, 2, 2));
            orderService.addOrder("1", "1", "1", "1", 1, details);
            orderService.addOrder("2", "2", "2", "2", 2, details);

            //订单内存在对象，故将其转化为另外一个对象，从而使输出更加合理
            detailbindingSource.DataSource = currentinfo;
            detailbindingSource.DataMember = "detailinfos";
            orderBindingSource.DataSource = orderinfos;
            dataGridView1.DataSource = orderBindingSource;
            dataGridView2.DataSource = detailbindingSource;

        }
        public void setOrderDataSource(List<Order> orders)
        {
            orderinfos.Clear();
            if (orders == null) {return; }
            foreach (var item in orders)
            {
                orderinfos.Add(new orderinfo(item.id, item.date, item.totalMoney.ToString(), item.client.name));

            }
            orderBindingSource = new BindingSource();
            orderBindingSource.DataSource = orderinfos;
            dataGridView1.DataSource = orderBindingSource;

           
        }
        public void setDetailDataSource(Order order)
        {
            currentinfo.detailinfos.Clear();
            if (order == null || order.details == null) {return; }
            foreach (var item in order.details)
            {
                currentinfo.detailinfos.Add(new Detailinfo(item.good.name, item.good.price.ToString(), item.amount.ToString()));
            }
            detailbindingSource = new BindingSource();
            detailbindingSource.DataSource = currentinfo.detailinfos;
            dataGridView2.DataSource = detailbindingSource;
            
        }
        private void setnull()
        {
            orderinfos.Clear();
            currentinfo.detailinfos.Clear();
            currentinfo.detailinfos.Add(new Detailinfo("", "", ""));
            orderinfos.Add(new orderinfo("","","",""));
            orderBindingSource = new BindingSource();
            orderBindingSource.DataSource = orderinfos;
            dataGridView1.DataSource = orderBindingSource;
            detailbindingSource = new BindingSource();
            detailbindingSource.DataSource = currentinfo.detailinfos;
            dataGridView2.DataSource = detailbindingSource;
        }

        private void neworder_Click(object sender, EventArgs e)
        {
            newDialog = new NewDialog();
            newDialog.Show();
            newDialog.confirm.Click+= new System.EventHandler(this.confirm_Click);
            //newDialog.Close();
            newDialog.cancel.Click += new System.EventHandler(this.cancel_Click);
        }
        private void confirm_Click(object sender, EventArgs e)
        {
            //newDialog
            try
            {
                if (newDialog == null) { return; }
                List<OrderDetail> details = new List<OrderDetail>();
                for (int i = 0; i < newDialog.count; i++)
                {
                    if (newDialog.TabTextsAmount[i].Text == null) { break; }
                    if (newDialog.TabTextsPrice[i].Text == null) { break; }
                    if (newDialog.TabTextsKind[i].Text == null) { break; }
                    details.Add(new OrderDetail(double.Parse(newDialog.TabTextsAmount[i].Text), double.Parse(newDialog.TabTextsPrice[i].Text), int.Parse(newDialog.TabTextsKind[i].Text)));
                }
                orderService.addOrder(newDialog.id.Text, newDialog.date.Text, newDialog.client.Text, "1", 1, details);
                newDialog.Close();
            }
            catch(System.FormatException exp)
            {
                MessageBox.Show(exp.ToString(),"输入错误");
            }


            
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            newDialog.Close();
        }

        private void changeCancel_Click(object sender, EventArgs e)
        {
            changeDialog.Close();
        }
        private void changeCon_Click(object sender, EventArgs e)
        {
            try
            {
                if (changeDialog == null) { return; }
                List<OrderDetail> details = new List<OrderDetail>();
                for (int i = 0; i < changeDialog.count; i++)
                {
                    if (changeDialog.TabTextsAmount[i].Text == null) { break; }
                    if (changeDialog.TabTextsPrice[i].Text == null) { break; }
                    if (changeDialog.TabTextsKind[i].Text == null) { break; }
                    details.Add(new OrderDetail(double.Parse(changeDialog.TabTextsAmount[i].Text), double.Parse(changeDialog.TabTextsPrice[i].Text), int.Parse(changeDialog.TabTextsKind[i].Text)));
                }
                orderService.changeOrder(1, changeid.Text, changeDialog.client.Text, 1, "1", details);
                orderService.changeOrder(2, changeid.Text, changeDialog.client.Text, 1, "1", details);
                changeDialog.Close();
            }
            catch (System.FormatException exp)
            {
                MessageBox.Show(exp.ToString(), "输入错误");
            }
            
        }
        private void change_Click(object sender, EventArgs e)
        {
            try
            {
                int.Parse(changeid.Text);
            }
            catch (System.FormatException exp)
            {
                MessageBox.Show("输入错误");
            }
            changeDialog = new ChangeDialog();
            changeDialog.Show();
            changeDialog.confirm.Click += new System.EventHandler(this.changeCon_Click);
            //newDialog.Close();
            changeDialog.cancel.Click += new System.EventHandler(this.changeCancel_Click);
        }

        private void import_Click(object sender, EventArgs e)
        {
            orderService.Import();
        }

        private void export_Click(object sender, EventArgs e)
        {
            orderService.Export();
        }

        private void delete_Click(object sender, EventArgs e)
        {
            bool ss = true;
            try
            {
                int.Parse(deletid.Text);
            }
            catch (System.FormatException exp)
            {
                MessageBox.Show("输入错误");
                ss = false;
            }
            orderService.removeOrder(deletid.Text);

            setnull();

            dataGridView1.Refresh(); dataGridView2.Refresh();
            if (ss) { MessageBox.Show("已删除"); }
        }

        private void query_Click(object sender, EventArgs e)
        {
            List<Order> orders = orderService.searchOrder(checkedRadio, queryinfo.Text, 1);
            if (orders != null)
            {
                setOrderDataSource(orders);
                setDetailDataSource(orders.First());
            }
            else
            {
                setnull();
                MessageBox.Show("未找到");
            }
            dataGridView1.Refresh(); dataGridView2.Refresh();
        }
        private void Check_changed(object sender, EventArgs e)
        {

            RadioButton button = sender as RadioButton;
            if (button == null) return;
            if (button.Checked)
            {
                checkedRadio = button.TabIndex + 1;
            }
        }
    }
    public class Detailinfo
    {
        public string good { set; get; }
        public string price { set; get; }
        public string amount { set; get; }
        public Detailinfo(string s1, string s2, string s3)
        {
           this.good = s1; this.price = s2; this.amount = s3;
        }
        public Detailinfo()
        {

        }
        public void change(string s1, string s2, string s3)
        {
            this.good = s1; this.price = s2; this.amount = s3;
        }
    }
    public class orderinfo
    {
        public orderinfo(string s1, string s2, string s3,string s4)
        {
            this.id = s1; this.data = s2; this.money = s3; this.client = s4;
        }
        public orderinfo() { detailinfos = new List<Detailinfo>(); }
        public string id { set; get; }
        public string data { set; get; }
        public string money { set; get; }
        public string client { set; get; }
        public List<Detailinfo> detailinfos { set; get; }
        public void change(string s1, string s2, string s3, string s4)
        {
            this.id = s1; this.data = s2; this.money = s3; this.client = s4;
        }
        public void add(Detailinfo detailinfo)
        {
            detailinfos.Add(detailinfo);
        }
        public void remove(Detailinfo detailinfo)
        {
            detailinfos.Remove(detailinfo);
        }
    }


}
